package com.example.financialsystem.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.financialsystem.R;
import com.example.financialsystem.RecordListActivity;

import java.util.List;

import models.StatisticInfo;

import static com.example.financialsystem.application.MyApplication.getContext;

public class RecyclerStatisticsAdapter extends RecyclerView.Adapter<RecyclerStatisticsAdapter.ViewHolder> {

    private List<StatisticInfo> mStatisticList;

    private Context context;

    static class ViewHolder extends RecyclerView.ViewHolder{
        View statisticView;
        ImageView imageTitle;
        TextView textTitle;
        TextView textRemark;
        TextView textIncome;
        TextView textOutcome;

        public ViewHolder(View view){
            super(view);
            statisticView = view;
            imageTitle = (ImageView)view.findViewById(R.id.recycler_statistic_image);
            textTitle = (TextView)view.findViewById(R.id.recycler_statistic_title);
            textRemark = (TextView)view.findViewById(R.id.recycler_statistic_remark);
            textIncome = (TextView)view.findViewById(R.id.recycler_statistic_income);
            textOutcome = (TextView)view.findViewById(R.id.recycler_statistic_outcome);
        }
    }

    public RecyclerStatisticsAdapter(List<StatisticInfo> statisticsList){
        mStatisticList = statisticsList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycler_book_statistics,parent,false);
        context = view.getContext();
        final ViewHolder holder = new ViewHolder(view);
        holder.statisticView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Intent intent = new Intent(getContext(), RecordListActivity.class);
                intent.putExtra("timeZoneType",position);
                intent.putExtra("statistic",mStatisticList.get(position));
                context.startActivity(intent);
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        StatisticInfo statistic = mStatisticList.get(position);
        String title = statistic.getName();
        String remark = statistic.getRemark();
        String income = String.valueOf(statistic.getIncome());
        String outcome = String.valueOf(statistic.getOutcome());
        switch (statistic.getType()){
            case 0:
                LoadDate(holder,R.drawable.icon_day,title,remark,income,outcome);
                break;
            case 1:
                LoadDate(holder,R.drawable.icon_week,title,remark,income,outcome);
                break;
            case 2:
                LoadDate(holder,R.drawable.icon_month,title,remark,income,outcome);
                break;
            case 3:
                LoadDate(holder,R.drawable.icon_year,title,remark,income,outcome);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mStatisticList.size();
    }

    public void LoadDate(ViewHolder holder,int imageResource,String title,String remark,
                         String income,String outcome){
        holder.imageTitle.setImageResource(imageResource);
        holder.textTitle.setText(title);
        holder.textRemark.setText(remark);
        holder.textIncome.setText(income);
        holder.textOutcome.setText(outcome);
    }

}